
var rts = require("/utils/rts.js");
App({
  //全局变量
  globalData: {
    apiUrl: "https://matols.com/",             //服务请求地址
    wxappId: "wxe89bfc7cxxxxxxxx",             //小程序id
    user: null,                                //用户信息（微信用户）
    info: null,                                //客户信息(业务用户)          
    session: null                              //业务系统鉴权域信息(一次获取，减少解密请求)
  },
  onLaunch: function () {
    var that = this;
    //初始化用户登录信息，并容错尝试
    //that.isOnline(function () {
    //console.log(that.globalData);      
    //});

    var taskList = [
      { id: 1, text: '创建个人的待办任务', done: false },
      { id: 2, text: '更加高效的管理自己的待办', done: true },
      { id: 3, text: '梳理工作思路，便于高效管理', done: false },
      { id: 4, text: '知道每天要做什么，不做什么', done: true },
      { id: 5, text: '事有轻重缓急', done: false },
      { id: 6, text: '工作学习，井井有条', done: true }
    ]

    wx.setStorageSync('taskList', taskList);

  },

  //用户在线状况
  isOnline: function (cb) {
    var that = this;
    if (that.globalData.session == null) {
      rts.initLogin(that.globalData, function () {  //登录授权信息
        that.globalData.info = wx.getStorageSync("info");
        that.globalData.session = wx.getStorageSync("session");
      }, function () {
        that.globalData.user = wx.getStorageSync("user");
        if (that.globalData.session == null) {
          rts.initLogin(that.globalData, function () {  //2次容错
            that.globalData.info = wx.getStorageSync("info");
            that.globalData.session = wx.getStorageSync("session");
          }, function () { if (typeof cb == "function") { cb(); } });
        } else { if (typeof cb == "function") { cb(); } }
      });
    } else {
      wx.checkSession({   //session登录态检测
        success: function () {
          if (typeof cb == "function") { cb(); }
        }, fail: function () {  //session失效重连
          rts.initLogin(that.globalData, function () {  //登录授权信息
            that.globalData.info = wx.getStorageSync("info");
            that.globalData.session = wx.getStorageSync("session");
          }, function () {
            that.globalData.user = wx.getStorageSync("user");
            if (that.globalData.session == null) {
              rts.initLogin(that.globalData, function () {  //2次容错
                that.globalData.info = wx.getStorageSync("info");
                that.globalData.session = wx.getStorageSync("session");
              }, function () { if (typeof cb == "function") { cb(); } });
            } else { if (typeof cb == "function") { cb(); } }
          });
        }, complete: function () {
          that.globalData.user = wx.getStorageSync("user");
          that.globalData.info = wx.getStorageSync("info");
          that.globalData.session = wx.getStorageSync("session");
        }
      })
    }
  },


  onShow: function () {
    //console.log('App Show')
  },
  onHide: function () {
    //console.log('App Hide')
  }
});

